package com.cn.web.role.service.impl;

import com.cn.role.RoleMenuDO;
import com.cn.user.UserRoleDO;
import com.cn.util.SnowflakeIdWorkerUtil;
import com.cn.web.role.mapper.IUserRoleMapper;
import com.cn.web.role.service.RoleMenuService;
import com.cn.web.role.service.UserRoleService;
import com.cn.web.role.vo.RoleVO;
import com.cn.web.user.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * description: UserRoleService <br>
 * date: 21.12.16 14:11 <br>
 * author: cn_yaojin <br>
 * version: 1.0 <br>
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    private IUserRoleMapper userRoleMapper;

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public void add(UserRoleDO userRoleEntity) {
        userRoleEntity.setId(SnowflakeIdWorkerUtil.getId());
        this.userRoleMapper.insert(userRoleEntity);
    }

    @Override
    public List<UserVO> listByRole(String roleId) {
        return this.userRoleMapper.listByRole(roleId);
    }

    @Override
    public List<UserRoleDO> listByUserId(String userId) {
        Map<String, Object> pd = new HashMap<>();
        pd.put("user_id", userId);
        return this.userRoleMapper.selectByMap(pd);
    }

    @Override
    public List<UserRoleDO> listByRoleId(String userId) {
        Map<String, Object> pd = new HashMap<>();
        pd.put("role_id", userId);
        return this.userRoleMapper.selectByMap(pd);
    }

    @Override
    public void del(String userId) {
        Map<String, Object> pd = new HashMap<>();
        pd.put("user_id", userId);
        this.userRoleMapper.deleteByMap(pd);
    }

    /**
     * 登录人查询自己的角色
     *
     * @param userId
     * @return
     */
    @Override
    public List<RoleVO> listRoleByUserId(String userId) {
        return this.userRoleMapper.listRoleByUserId(userId);
    }

    /**
     * 封装角色-菜单的数据权限
     *
     * @param userId
     */
    @Override
    public Map<String /*menuId*/, Integer /*dataType*/> packMenuPermission(String userId) {
        List<RoleVO> roles = listRoleByUserId(userId);
        List<RoleMenuDO> menus = null;
        Map<String /*menuId*/, Set<Integer> /*dataType*/> menuMap = new HashMap<>();
        for (RoleVO role : roles) {
            menus = this.roleMenuService.list(role.getId().toString());
            if (menus != null) {
                for (RoleMenuDO menu : menus) {
                    Set<Integer> tmp = menuMap.get(menu.getMenuId());
                    if (tmp == null) {
                        tmp = new HashSet<>();
                        menuMap.put(menu.getMenuId(), tmp);
                    }
                    if (StringUtils.isNotEmpty(role.getDataType())) {
                        tmp.add(Integer.valueOf(role.getDataType()));
                    } else {
                        tmp.add(2);
                    }
                }
            }
        }

        Map<String /*menuId*/, Integer /*dataType*/> menuPermission = new HashMap<>();
        for (String s : menuMap.keySet()) {
            Set<Integer> keys = menuMap.get(s);
            int i = 0;
            for (Integer k : keys) {
                if (i == 0) {
                    menuPermission.put(s, k);
                    break;
                }
                i++;
            }
        }
        return menuPermission;
    }

}
